package controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import po.Message;
import websocket.MyWebSocketHandler;

import com.google.gson.GsonBuilder;

@Controller
public class ChatController {
	@Autowired
	MyWebSocketHandler handler;
	
	
	@RequestMapping("/onlineusers")
	@ResponseBody
	public Set<String> onlineusers(HttpSession session){
		Map<String, WebSocketSession> map=MyWebSocketHandler.userSocketSessionMap;
		Set<String> set=map.keySet();
		Iterator<String> it = set.iterator();
		Set<String> nameset=new HashSet<String>();
		while(it.hasNext()){
			String entry = it.next();
//			String name=loginservice.getnamebyid(entry);
			String uid=(String)session.getAttribute("uid");
			nameset.add(uid);
		}
		return nameset;
	}
	
	// 发布系统广播（群发）
	@ResponseBody
	@RequestMapping(value = "broadcast", method = RequestMethod.POST)
	public void broadcast(@RequestParam("text") String text) throws IOException {
		Message msg = new Message();
		msg.setDate(new Date());
		msg.setFrom("-1");//-1表示系统广播
		msg.setFromName("系统广播");
		msg.setTo("0");
		msg.setText(text);
		handler.broadcast(new TextMessage(new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().toJson(msg)));
	}
		
//	@RequestMapping("getuid")
//	@ResponseBody
//	public User getuid(@RequestParam("username")String username){
//		String a=loginservice.getUidbyname(username);
//		User u=new User();
//		u.setUid(a);
//		return u;
//	}
}
